﻿using UnityEngine;
using System.Collections;


public class HarvesterBehavior : BehaviorUnits 
{
    private Vector3 _ressourcePointSelected = Vector3.zero;
    private bool _isHarvesting = false;
    private int _nbRessource = 0;

    public int _amountOfRessourceMax = 10;
    public int _harvestingSpeed = 2; // units per sec

    public override void SetDestination(Vector3 destination, string tagClick)
    {

        base.SetDestination(destination, tagClick);
        if (tagClick == ListTags.tagRessource)
        {
            _ressourcePointSelected = destination;
            SetState(State.Havesting);
        }
        else if (tagClick == ListTags.tagTotem)
        {
            _ressourcePointSelected = Vector3.zero;
            SetState(State.Deposit);
        }
        else
        {
            _ressourcePointSelected = Vector3.zero;
        }
    }

    public override void StateHarvesting(FsmStateEvent eEvent)
    {
 	    switch (eEvent)
        {
            case FsmStateEvent.eEnter:
                {
                    _navMeshAgent.SetDestination(_ressourcePointSelected);
                    //play anim
                }
                break;
            case FsmStateEvent.eUpdate:
                {
                    if (_nbRessource >= _amountOfRessourceMax)
                    {
                        SetState(State.Deposit);
                    }
                    else if (Vector3.Distance(_ressourcePointSelected,this.transform.position) < 1.5f && !_isHarvesting)
                    {
                        _navMeshAgent.Stop();
                        StartCoroutine(Harvest());
                        _isHarvesting = true;
                    }
                }
                break;
        }
    }

    public override void StateDeposit(FsmStateEvent eEvent)
    {
        switch (eEvent)
        {
            case FsmStateEvent.eEnter:
                {
                    _navMeshAgent.SetDestination(_Totem.transform.position);
                }
                break;
            case FsmStateEvent.eUpdate:
                {
                    if (Vector3.Distance(this.transform.position, _Totem.transform.position) < 1.5f)
                    {
                        _navMeshAgent.Stop();
                        HandleRessource._totalRessource += _nbRessource;
                        _nbRessource = 0;

                        if (_ressourcePointSelected != Vector3.zero) 
                            SetState(State.Havesting);
                        else
                            SetState(State.Idle); // if exit from a direct order to the totem
                    }
                    else if (_navMeshAgent.destination.x != _Totem.transform.position.x || _navMeshAgent.destination.z != _Totem.transform.position.z)
                    {
                        _navMeshAgent.SetDestination(_Totem.transform.position);
                    }

                }
                break;
        }
    }




    IEnumerator Harvest()
    {
        while(fsm.GetState() == (uint)State.Havesting)
        {
            yield return new WaitForSeconds(0.5f);
            if (fsm.GetState() == (uint)State.Havesting)
            {
                int ressourceTaken = _scriptCloud.TakeRessource(1); //Call ONLY once. Remove ressource from cloud.
                if (ressourceTaken > 0)
               {
                   _nbRessource += ressourceTaken;
               }
                else if (_nbRessource <= 0) //If point is empty and harvester has no ressource
                {
                    SetState(State.Idle);
                }
                else
                {
                    SetState(State.Deposit);
                }
            }
        }
        _isHarvesting = false;
    }

   
}